package com.yurun.controller;

import com.yurun.common.Result;
import com.yurun.entity.Student;
import com.yurun.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 宫静雨
 */
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping("/YR100104")
    public Result add(@RequestBody Student student) {
        if (Objects.isNull(student.getStudentId())) {
            throw new IllegalArgumentException();
        }
        return studentService.save(student) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @PostMapping("/YR100103")
    public Result modify(@RequestBody Student student) {
        if (Objects.isNull(student.getStudentId())) {
            throw new IllegalArgumentException();
        }
        return studentService.updateById(student) ?
                Result.res("000000", "Succeeded") :
                Result.res("000001", "Fail");
    }

    @GetMapping("/YR100102")
    public Result getById(@RequestParam(name = "studentId") Integer studentId) {
        final Student student = studentService.getById(studentId);
        return Result.res("000000", "Succeeded")
                .setData(student);
    }

    @GetMapping("/YR100101")
    public Result list2(@RequestParam(required = false) List<Integer> studentId,
                        @RequestParam(required = false) String studentName,
                        @RequestParam(required = false) List<Integer> classId) {
        final List<Student> students = studentService.listStudent(studentId, studentName, classId);
        return Result.res("000000", "Succeeded")
                .setData(students);
    }
}
